package com.example.userconsumer.controller;

import com.example.userconsumer.feign.UserFeign;
import com.example.userconsumer.utils.JwtUtil;
import com.myshop.b2b.commonmodule.User;
import com.myshop.b2b.commonmodule.vo.Result;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author likaiping
 * @createTime 2024/7/2 19:17
 */
@RestController
@RequestMapping("/admin")
@AllArgsConstructor
@RefreshScope //自动刷新bean属性 不用重启
public class UserController {
    private UserFeign userFeign;
    private RedisTemplate redisTemplate;
    @GetMapping(value = "/tologin")
    public String tologin() {
        return "login";
    }

    // 跳转注册界面
    @GetMapping(value = "/toregister")
    public String toregister() {
        return "register";
    }

    // 用户登录
    @GetMapping(value = "/login")
    public Result login(String uname, String upassword, HttpServletRequest request) {
        System.out.println("用户名：" + uname + "密码：" + upassword);
        User user = userFeign.login(uname);
        System.out.println(user);
        if (user != null) {
            System.out.println("user不为空");
            if (user.getUpassword().equals(upassword)){
                //登录成功 生成token
                Map<String,Object> claims = new HashMap<>();
                claims.put("uaccount",user.getUaccount());
                claims.put("username",user.getUname());
                String token = JwtUtil.getToken(claims);
                System.out.println(token);
                //把token存储到redis中
                ValueOperations<String, String> operations = redisTemplate.opsForValue();
                // token作为键和值，可以使每个token都能存进去，“token”为键的话会覆盖
                operations.set(token,token,4, TimeUnit.HOURS);//把token存储后设置过期时间为4h
                HttpSession session = request.getSession(true);
                session.setAttribute("user", user);  //将登陆者信息存入session

                System.out.print("登陆成功！");
                return Result.success(token);
                /*return "redirect:http://localhost:8896/goods/getAll?uaccount="+user.getUaccount()+
                        "&upasswoed="+user.getUpassword();*/
            }
        }
        System.out.print("登录失败");
        return Result.error("用户名或密码错误");
//        return "/login";
    }
    //用户注册
    @GetMapping(value = "/register")
    public Result register(HttpServletRequest request,String uname,String upassword,
                           String usex) {
        if(userFeign.register(upassword, uname, usex)>0){
            System.out.print("注册成功");
//            return "login";
            return Result.success("注册成功");
        }
        System.out.print("注册失败");
//        return "register";
        return Result.error("注册失败");
    }
}
